<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工程识别号" prop="engNo" label-width="30">
        <el-input
          v-model="queryParams.engNo"
          placeholder="请输入工程识别号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="工程名称" prop="engName">
        <el-input
          v-model="queryParams.engName"
          placeholder="请输入工程名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="comments">
        <el-input
          v-model="queryParams.comments"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="ctime">
        <el-date-picker clearable
          v-model="queryParams.ctime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="归档状态" prop="completeFlag">
        <el-select v-model="queryParams.completeFlag" placeholder="请选择归档状态" clearable >
          <el-option
            v-for="dict in dict.type.complete_flag_dict"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"/>
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.completeFlag"-->
<!--          placeholder="请输入是否归档"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
<!--      <el-form-item label="预留 1" prop="engSerialReserve1">-->
<!--        <el-input-->
<!--          v-model="queryParams.engSerialReserve1"-->
<!--          placeholder="请输入预留 1"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="预留 2" prop="engSerialReserve2">-->
<!--        <el-input-->
<!--          v-model="queryParams.engSerialReserve2"-->
<!--          placeholder="请输入预留 2"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增</el-button>
<!--        v-hasPermi="['system:serial:add']"-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >修改</el-button>
<!--        v-hasPermi="['system:serial:edit']"-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除</el-button>
<!--        v-hasPermi="['system:serial:remove']"-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出</el-button>
<!--        v-hasPermi="['system:serial:export']"-->
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serialList" @selection-change="handleSelectionChange" ref="multipleTable" @row-click="handleRowClick">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="表主键" align="center" prop="engId" />-->
      <el-table-column label="工程识别号" align="center" prop="engNo" />
      <el-table-column label="工程名称" align="center" prop="engName" />
      <el-table-column label="备注" align="center" prop="comments" />
      <el-table-column label="创建时间" align="center" prop="ctime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ctime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否归档" align="center" prop="completeFlag" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.complete_flag_dict" :value="scope.row.completeFlag"/>
          <!--          <span>{{ getFormattedReturnFlag(scope.row.returnflag) }}</span>-->
        </template>
      </el-table-column>
<!--      <el-table-column label="预留 1" align="center" prop="engSerialReserve1" />-->
<!--      <el-table-column label="预留 2" align="center" prop="engSerialReserve2" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改</el-button>
<!--          v-hasPermi="['system:serial:edit']"-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除</el-button>
<!--          v-hasPermi="['system:serial:remove']"-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工程识别号对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal='false'>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="工程识别号" prop="engNo">
          <el-input v-model="form.engNo" placeholder="请输入工程识别号" >
            <!-- 使用 append 插槽在 el-input 右侧添加按钮 -->
            <template slot="append">
              <el-button @click="generateEngNo">生成工程识别号</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="工程名称" prop="engName">
          <el-input v-model="form.engName" placeholder="请输入工程名称" />
        </el-form-item>
        <el-form-item label="备注" prop="comments">
          <el-input v-model="form.comments" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="创建时间" prop="ctime">
          <el-date-picker clearable
            v-model="form.ctime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="归档状态" prop="completeFlag">
          <el-select v-model="form.completeFlag" placeholder="请选择是否归档" >
            <el-option
              v-for="dict in dict.type.complete_flag_dict"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"/>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="预留 1" prop="engSerialReserve1">-->
<!--          <el-input v-model="form.engSerialReserve1" placeholder="请输入预留 1" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="预留 2" prop="engSerialReserve2">-->
<!--          <el-input v-model="form.engSerialReserve2" placeholder="请输入预留 2" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSerial, getSerial, delSerial, addSerial, updateSerial } from "@/api/system/serial";


export default {
  name: "Serial",
  dicts: [ 'complete_flag_dict'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程识别号表格数据
      serialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        engNo: null,
        engName: null,
        comments: null,
        ctime: null,
        completeFlag: null,
        engSerialReserve1: null,
        engSerialReserve2: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    generateEngNo() {
      const generateRandomHex = (length) => {
        let result = '';
        const characters = '0123456789abcdef';
        const charactersLength = characters.length;
        for (let i = 0; i < length; i++) {
          result += characters.charAt(Math.floor(Math.random() * charactersLength));
        }
        return result;
      };
      // 生成随机的 UUID 格式字符串
      const uuid = `${generateRandomHex(8)}-${generateRandomHex(4)}-${generateRandomHex(4)}-${generateRandomHex(4)}-${generateRandomHex(12)}`;
      this.form.engNo = uuid;
    },
    /** 查询工程识别号列表 */
    getList() {
      this.loading = true;
      listSerial(this.queryParams).then(response => {
        this.serialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        engId: null,
        engNo: null,
        engName: null,
        comments: null,
        ctime: null,
        completeFlag: null,
        engSerialReserve1: null,
        engSerialReserve2: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.engId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工程识别号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const engId = row.engId || this.ids
      getSerial(engId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工程识别号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.engId != null) {
            updateSerial(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSerial(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const engIds = row.engId || this.ids;
      this.$modal.confirm('是否确认删除工程识别号编号为"' + engIds + '"的数据项？').then(function() {
        return delSerial(engIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/serial/export', {
        ...this.queryParams
      }, `serial_${new Date().getTime()}.xlsx`)
    },
    //点击行触发，选中或不选中复选框
    handleRowClick (row, column, event) {
      this.$refs.multipleTable.toggleRowSelection(row);
    },

  }
};
</script>
